'''Packet protocol implemented by individual nodes.

Protocol is request-response, both server and client side are impleemnted here.
Nothing but a simple ping for now.
'''
import struct

def handler(data, addr):
    id,data = data[:1],data[1:]
    id = ord(id)
    if id is 0: # PING
        if data=='ping':
            return 'pong'
        else:
            raise RuntimeError('Malformed ping packet received from '+str(addr))
    else:
        raise RuntimeError('Invalid request received from '+str(addr))
    
def ping():
    'Builds a ping request.'
    return '\0ping'
